package com.ewhale.feitengguest.ui.shop.deal

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.dto.WaitHandleDto
import com.ewhale.feitengguest.presenter.shop.deal.SDealPresenter
import com.ewhale.feitengguest.ui.shop.deal.adapter.DealAdapter
import com.ewhale.feitengguest.view.shop.deal.SDealView
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.simga.library.activity.MBaseFragment
import com.simga.library.utils.LogUtil
import kotlinx.android.synthetic.main.fragment_deal.*

/**
 * 作者：SimGa
 * 日期：2020/1/2
 * 时间：11:14
 * 包名：com.ewhale.feitengguest.ui.shop.deal
 */
class DealFragment : MBaseFragment<SDealPresenter>(), SDealView {
    override fun getViewId(): Int {
        return R.layout.fragment_deal
    }

    private val mAdapter by lazy(LazyThreadSafetyMode.NONE) { DealAdapter() }
    private var pageNumber = 1
    private val myReceiver by lazy(LazyThreadSafetyMode.NONE) { MyReciver() }


    override fun init(savedInstanceState: Bundle?) {
        val filter = IntentFilter()
        filter.addAction("UPDATE_SUBMIT_LIST")
        activity!!.registerReceiver(myReceiver, filter)
        listview.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
        }
        xloading_view.showLoading()
        presenter.loadTaskList(pageNumber)
    }

    override fun showDealList(list: List<WaitHandleDto>) {
        if (pageNumber == 1) {
            mAdapter.setNewData(list)
        } else {
            mAdapter.addData(list)
        }
        if (mAdapter.mData.size == 0) {
            xloading_view.showEmpty()
        } else {
            xloading_view.showContent()
        }
        onLoad(list.size)
    }

    private fun onLoad(size: Int) {
        if (ref_layout == null) {
            return
        }
        ref_layout.finishRefreshLoadingMore()
        if (size < 10) {
            ref_layout.enableLoadMore(false)
        } else {
            ref_layout.enableLoadMore(true)
        }
    }

    override fun initListener() {
        super.initListener()
        ref_layout.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                super.onLoadMore(refreshLayout)
                pageNumber++
                presenter.loadTaskList(pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                pageNumber = 1
                presenter.loadTaskList(pageNumber)
            }
        })
        mAdapter.setOnItemClickListener { _, item, _ ->
            HandlerTaskActivity.open(mContext, item.id)
        }
        xloading_view.setOnRetryClickListener {
            pageNumber = 1
            xloading_view.showLoading()
            presenter.loadTaskList(pageNumber)
        }
    }

    override fun showProLoading() {
        showLoading()
    }
    override fun dimissProLoading() {
        dismissLoading()
    }

    override fun showErrorMessage(postCode: Int, code: String, message: String) {
        showErrorMsg(code, message)
        onLoad(-1)
        if (postCode == 1 || postCode == -10086) {
            xloading_view.showError()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mAdapter.cancelAllTimers()
        activity!!.unregisterReceiver(myReceiver)
    }

    inner class MyReciver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent!!.action == "UPDATE_SUBMIT_LIST") {
                pageNumber = 1
                presenter.loadTaskList(pageNumber)
            }
        }
    }
}
